//
//  YCVCsPopTransition.m
//  DemoTransition
//
//  Created by yunsheng on 15/7/22.
//  Copyright (c) 2015年 yunsheng. All rights reserved.
//

#import "YCVCsPopTransition.h"

#import "YCPushPopViewController.h"
#import "YCPushPopDetailViewController.h"

@implementation YCVCsPopTransition

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.5;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    YCPushPopDetailViewController *fromVC = (YCPushPopDetailViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    YCPushPopViewController *toVC = (YCPushPopViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    
    UIView *snapshotView = [fromVC.imgView snapshotViewAfterScreenUpdates:NO];
    snapshotView.frame = [containerView convertRect:fromVC.imgView.frame fromView:fromVC.view];
    fromVC.imgView.hidden = YES;
    
    toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
    toVC.selectedCell.imageView.hidden = YES;
    
    [containerView insertSubview:toVC.view belowSubview:fromVC.view];
    [containerView addSubview:snapshotView];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        snapshotView.frame = [containerView convertRect:toVC.selectedCell.imageView.frame fromView:toVC.selectedCell];
        fromVC.view.alpha = 0;
    } completion:^(BOOL finished) {
        toVC.selectedCell.imageView.hidden = NO;
        [snapshotView removeFromSuperview];
        fromVC.imgView.hidden = NO;
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

@end
